//
//  DHProductCollectionViewCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/9.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHProductCollectionViewCell.h"
#import "Product.h"
#import "DHSizeSelectViewController.h"

@interface DHProductCollectionViewCell()

@property (weak, nonatomic) IBOutlet UIImageView *coverImgView;
@property (weak, nonatomic) IBOutlet UIButton *addButton;
@property (weak, nonatomic) IBOutlet UILabel *addedLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *brandLabel;
@property (weak, nonatomic) IBOutlet UILabel *desLabel;
@property (weak, nonatomic) IBOutlet UIButton *collectionButton;
@property (weak, nonatomic) IBOutlet UILabel *stockoutLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *priceViewWidthLayout;


@property (strong, nonatomic) Product *product;

@end

@implementation DHProductCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    WeakSelf(weakSelf)
    [self.collectionButton bk_addEventHandler:^(id sender) {
        weakSelf.product.liked = [NSNumber numberWithBool:![self.product.liked boolValue]];
        [weakSelf changeLikeButtonImg];
        [[DHNetAPIClient shareDHNetAPIClient] request_user_likeAndOrigin:self.product._id andBlock:^(id data, NSError *error) {
            
        }];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateWithProduct:(Product *)product{
    self.product = product;
    if (product.snapshots.count > 0) {
        [self.coverImgView dh_setImageURLString:product.snapshots[0]];
    }else{
        [self.coverImgView dh_setImageURLString:@""];
    }
    [self changeLikeButtonImg];
    
    self.brandLabel.text = [NSString removeSpacesAndLinebreaks:product.brand.name];
    self.desLabel.text = product.name;
    
    __block BOOL isStockout = YES;
    [self.product.sizes enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            self.priceLabel.text = [NSString stringWithFormat:@"￥%.1f",[[obj objectForKey:@"lease_price"] floatValue]];
        }
        if ([[obj objectForKey:@"inventory"] integerValue] - [[obj objectForKey:@"locked"] integerValue] > 0) {
            isStockout = NO;
        }
    }];
    if (isStockout) {
        self.stockoutLabel.hidden = NO;
    }else{
        self.stockoutLabel.hidden = YES;
    }
    
    CGFloat w = [self.priceLabel.text widthForFont:self.priceLabel.font];
    self.priceViewWidthLayout.constant = w + 37;
}

- (void)changeLikeButtonImg{
    if ([self.product.liked boolValue]) {
        [self.collectionButton setImage:[UIImage imageNamed:@"product_like"] forState:UIControlStateNormal];
    }else{
        [self.collectionButton setImage:[UIImage imageNamed:@"product_unlike"] forState:UIControlStateNormal];
    }
}
- (IBAction)addToCartAction:(id)sender {
    DHSizeSelectViewController *ssControllr = [[DHSizeSelectViewController alloc] initWithSizesArray:self.product.sizes];
    [self.viewController presentViewController:ssControllr animated:YES completion:nil];
}

@end
